// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Jeff Stewart
// Entry point (main or WinMain) for the POP and IMAP troubleshooter.

#ifdef _WIN32
#include <windows.h>
#endif

#include <QtCore/QLocale>
#include <QtCore/QString>
#include <QtCore/QTranslator>
#include <QtGui/QApplication>
#include <QtGui/QIcon>
#include <QtNetwork/QHostInfo>

#include <cstdlib>
#include <ctime>

#include "ui/wizard.h"
#include "util/common.h"
#include "util/provider.h"

namespace popdiag {

class Provider *provider;

// Setup the UI translations, either based on the system locale or based on
// an override provided on the command line.
void ConfigureTranslations(QApplication *app, int argc, char **argv) {
  // Check for pseudo translation.
  QString translation_target = QLocale::system().name();
  for (int i = 1; i < argc; ++i) {
    QString arg(argv[i]);
    if (arg.startsWith("--translation=")) {
      // with KeepEmptyParts, if there's nothing after the '=', we get an
      // empty string in slot 2 (as opposed to only one slot)
      translation_target = arg.split("=", QString::KeepEmptyParts)[1];
    }
  }

  if (translation_target != "C" && translation_target != "POSIX") {
    // Translator is Qt-owned by app.
    QTranslator *translator = new QTranslator(app);
      // Unix system locales that do not require translations.
    if (translator->load(":/translations/popdiag_" + translation_target)) {
        app->installTranslator(translator);
    } else {
      // This will not show up on Windows since this is not a "console"
      // application. But it also doesn't warrant a message box.
      fprintf(stderr, "warning: cannot load translation for %s\n",
              qPrintable(translation_target));
    }
  }
}

// Setup global QApplication defaults.  App name, icon, fonts, etc.
QApplication *ConfigureApp(int &argc, char **argv) {
  QApplication *app = new QApplication(argc, argv);
  ConfigureTranslations(app, argc, argv);
  provider = InitProvider();
  app->setApplicationName(QObject::tr("POP and IMAP Troubleshooter"));
  // do NOT translate the company name and internet domain:
#ifdef COMPANY_NAME
  app->setOrganizationName(COMPANY_NAME);
#endif
#ifdef COMPANY_DOMAIN
  app->setOrganizationDomain(COMPANY_DOMAIN);
#endif
  app->setQuitOnLastWindowClosed(true);
#ifdef WINDOW_ICON
  app->setWindowIcon(QIcon(WINDOW_ICON));
#endif
  QFont default_font = app->font();
  // Use a sans-serif font.  Do NOT translate the font name:
  default_font.setFamily("Arial");
  app->setFont(default_font);
  return app;
}

}  // namespace popdiag

int main(int argc, char **argv) {
  srand(time(NULL));
  Q_INIT_RESOURCE(resources);
#ifdef INIT_BRANDING_RESOURCE
  INIT_BRANDING_RESOURCE;
#endif
  popdiag::scoped_ptr<QApplication> app(
    popdiag::ConfigureApp(argc, argv));
  int rval = 0;
  {
    popdiag::Wizard wizard(NULL);
    app->exec();
  }
  return rval;
}
